import * as commonService from '../../services/common';
import {queryRoomList} from '../../services/tRoom';
import { routerRedux } from 'dva/router';
import { message } from 'antd';

export default {
  namespace: 'index',
  state: {
    loginLoading: false,
    realName: '',
    confirmLoading: false,
    newKey : '',
    alarmCount:'',
    assetCount:'',
    rackCount:'',
    uCount:'',
    scrapCount:'',
    presetCount:'',
    roomData:[],
    roomCount:'',
    uData:[],
    roomList:[],
    updata: "index/assetCountByRoom",
    assetCountByRoom:[],
    total:'',

  },
  reducers: {   
    countSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryUcountSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryAssetCountSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryAlarmCountSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryPresetCountSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryUPerByRoomSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    qryScrapCountSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    assetCountByRoomSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    
  },
  effects: {

    *count({ payload },{ call, put }) {
      const { body }  = yield call(commonService.roomCount, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'countSuc',
          payload: {
            roomCount: body.data,
            rackCount: body.rackCount,
          },
      });
      yield put({ type: 'queryRoom'});
      yield put({type: 'assetCountByRoom', payload:"0"});     
      }
    },

    *qryUcount({ payload },{ call, put }) {
      const { body }  = yield call(commonService.qryUcount, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'qryUcountSuc',
          payload: {
            uCount:body.data,
          },
        });
      }
    },

    *qryAssetCount({ payload },{ call, put }) {
      const { body }  = yield call(commonService.qryAssetCount, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'qryAssetCountSuc',
          payload: {
            assetCount:body.data,
          },
        });  
      }
    },

    *qryAlarmCount({ payload },{ call, put }) {
      const { body }  = yield call(commonService.qryAlarmCount, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'qryAlarmCountSuc',
          payload: {
            alarmCount: body.data,
          },
        });        
      }
    },

    *qryPresetCount({ payload },{ call, put }) {
      const { body }  = yield call(commonService.qryPresetCount, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'qryPresetCountSuc',
          payload: {
            presetCount: body.data,
            roomData: body.roomData,
          },
        });       
      }
    },

    *qryUPerByRoom({ payload },{ call, put }) {
      const { body }  = yield call(commonService.qryUPerByRoom, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'qryUPerByRoomSuc',
          payload: {
            uData: body.data,
          },
        });
      }
    },

    *qryScrapCount({ payload },{ call, put }) {
      const { body }  = yield call(commonService.qryScrapCount, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'qryScrapCountSuc',
          payload: {
            scrapCount: body.data,
          },
        });
      }
    },
    *assetCountByRoom({ payload },{ call, put }) {
      const { body }  = yield call(commonService.assetCountByRoom, payload);
      if(body.httpCode === 200){
        yield put({
          type: 'assetCountByRoomSuc',
          payload: {
             assetCountByRoom:body.data,
             total:body.total,
          },
      });
      }
    },
    
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
  },
  subscriptions: {
      setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({ type: 'count', payload:{}});
          dispatch({ type: 'qryUPerByRoom', payload:{}});
          dispatch({ type: 'qryUcount', payload:{}});
          dispatch({ type: 'qryAssetCount', payload:{}});
          dispatch({ type: 'qryAlarmCount', payload:{}});
          dispatch({ type: 'qryPresetCount', payload:{}});          
          dispatch({ type: 'qryScrapCount', payload:{}});
        }
      });
    },
  },
};
